package com.kasax.skin.support.corel;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.kasax.skin.support.utils.LogUtils;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

public class SkinLayoutFactory implements LayoutInflater.Factory2, Observer {
    // 属性处理类

    private final SkinAttribute skinAttribute;
    private final Activity mContext;

    public SkinLayoutFactory(Activity context, Typeface skinTypeFace) {
        this.mContext = context;
        skinAttribute = new SkinAttribute(skinTypeFace);
    }

    private static final String[] mClassPrefixList = {
            "android.widget.",
            "android.view.",
            "android.webkit."
    };

    private static final Class[] mConstructorSignature = new Class[]{
            Context.class, AttributeSet.class
    };

    // 构造方法签名列表
    private static final HashMap<String, Constructor<? extends View>> mConstructors = new HashMap<>();

    public static SkinLayoutFactory create(Activity context, Typeface skinTypeFace) {
        return new SkinLayoutFactory(context, skinTypeFace);
    }

    @Nullable
    @Override
    public View onCreateView(@Nullable View parent, @NonNull String name, @NonNull Context context,
                             @NonNull AttributeSet attrs) {
        LogUtils.i(getClass(), "onCreateView");

        View view = onCreateViewFromTag(name, context, attrs);
        if (null == view) {
            view = createView(name, context, attrs);
        }
        // 筛选符合属性的view
        skinAttribute.load(view, attrs);
        return view;
    }

    private View onCreateViewFromTag(String name, Context context, AttributeSet attrs) {
//        LogUtils.i(getClass(), "onCreateViewFromTag name: " + name);
        if (-1 != name.indexOf(".")) {
            // 包含自定义控件
            return null;
        }
        View view = null;
        for (int i = 0; i < mClassPrefixList.length; i++) {
            view = createView(mClassPrefixList[i] + name, context, attrs);
//            LogUtils.i(getClass(), "onCreateViewFromTag view: " + view);
            if (null != view) {
                break;
            }
        }
        return view;
    }

    private View createView(String name, Context context, AttributeSet attrs) {
//        LogUtils.i(getClass(), "createView name: " + name);
        Constructor<? extends View> constructor = mConstructors.get(name);
        if (constructor == null) {
            try {
                // 通过全类名获取class
                Class<? extends View> aClass = context.getClassLoader().loadClass(name).asSubclass(View.class);
                // 获取构造方法
                constructor = aClass.getConstructor(mConstructorSignature);
                mConstructors.put(name, constructor);
            } catch (Exception e) {
                LogUtils.e(getClass(), e);
            }
        }
        if (null != constructor) {
            try {
                return constructor.newInstance(context, attrs);
            } catch (Exception e) {
                LogUtils.e(getClass(), e);
            }
        }
        return null;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull String name, @NonNull Context context, @NonNull AttributeSet attrs) {
        LogUtils.i(getClass(), "onCreateView");
        return null;
    }

    @Override
    public void update(Observable o, Object arg) {
        // 更新状态栏皮肤
        SkinThemeUtils.updateStatusBarColor(mContext);
        // 更新字体皮肤
        Typeface skinTypeface = SkinThemeUtils.getSkinTypeface(mContext);
        skinAttribute.setSkinTypeface(skinTypeface);

        // 更换皮肤
        if (skinAttribute != null) {
            skinAttribute.applySkin();
        }
    }
}
